package com.wisdytech.linkdcs.system.action;

import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.handler.ResourceMatchingService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.system.service.ISystemLoginManageService;
import com.wisdytech.linkdcs.system.vo.LoginVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.session.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 
 * @author zhanghailin
 *
 */
@Controller
@RequestMapping("/system/loginManage")
public class SystemLoginManagerAction {
	
	private static final Logger logger = LoggerFactory.getLogger(SystemLoginManagerAction.class);
	
	
	@Resource
	private ISystemLoginManageService systemLoginManageService;
    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;
	@RequestMapping("/index")
    public String index(Model model, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        return "system/loginManage/loginManageIndex";
    }
	
	/**
     * <p>Description: 根据查询条件查询全部登录信息（状态为1,即登录中）</p>
     * 
     * @param userName 用户名
     * @param trueName 姓名
     * @param ipAddress 地址
     * @param filter 分页参数
     * @return 登录信息列表集合
     */
    @ResponseBody
    @RequestMapping(value = "/listSysLoginManages/get.mvc")
    public StandardTableResult<LoginVO> listSysLoginManages(String userName, String trueName, String ipAddress,Filter filter) {
        return systemLoginManageService.getLoginUsers(userName, trueName, ipAddress, filter);
    }
    
    /**
     * 
     * <p>Description: 强制踢出登录用户</p>
     * @param sessionId sessionId
     * @return 操作结果
     */
    @ResponseBody
    @RequestMapping(value = "/logOffUser/logOff.mvc")
    public StandardResult logOffUser(String sessionId) {
        logger.info(">>>>>>>>>>>>>>强制注销用户，sessionId:{}<<<<<<<<<<<<<<<<<<<<<<",sessionId);
        Session currentSession = BaseUtil.getCurrentSession();
        if(StringUtils.isNotBlank(sessionId)&&sessionId.equals(currentSession.getId())) {
            return StandardResult.getFailResult("请不要注销自己...");
        }
        systemLoginManageService.logOffUserBySessionId(sessionId);
        return StandardResult.getSuccessResult();
    }

}
